/**
 * DOM操作工具类
 */
class DomUtil {
    /**
     * 安全获取DOM元素
     * @param {string} selector - CSS选择器
     * @param {number} timeout - 超时时间(ms)
     */
    static async waitElement(selector, timeout = 5000) {
        return new Promise((resolve, reject) => {
            const start = Date.now();
            const check = () => {
                const el = document.querySelector(selector);
                if (el) return resolve(el);
                if (Date.now() - start > timeout) {
                    reject(new Error(`Element ${selector} not found`));
                }
                requestAnimationFrame(check);
            };
            check();
        });
    }

    /**
     * 批量添加事件监听
     * @param {string} selector - CSS选择器
     * @param {string} eventType - 事件类型
     * @param {Function} handler - 处理函数
     */
    static delegate(selector, eventType, handler) {
        document.addEventListener(eventType, function (e) {
            if (e.target.matches(selector)) {
                handler(e);
            }
        });
    }
    static getById(id) { return document.getElementById(id); }
    static getOne(selector, base = document) { return base.querySelector(selector); }
    static getAll(selector, base = document) { return base.querySelectorAll(selector); }

}

// 导出DomUtil类
module.exports = DomUtil;